package classesJpa;

import classesBancodeDados.Responsavel;
import classesJpa.exceptions.NonexistentEntityException;
import classesJpa.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

public class ResponsavelJpaController implements Serializable {

    public ResponsavelJpaController() {
        emf = Persistence.createEntityManagerFactory("Juliano_MoreiraPU");
    }
    
    public ResponsavelJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Responsavel responsavel) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(responsavel);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findResponsavel(responsavel.getCpf()) != null) {
                throw new PreexistingEntityException("Responsavel " + responsavel + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Responsavel responsavel) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            responsavel = em.merge(responsavel);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = responsavel.getCpf();
                if (findResponsavel(id) == null) {
                    throw new NonexistentEntityException("The responsavel with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Responsavel responsavel;
            try {
                responsavel = em.getReference(Responsavel.class, id);
                responsavel.getCpf();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The responsavel with id " + id + " no longer exists.", enfe);
            }
            em.remove(responsavel);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Responsavel> findResponsavelEntities() {
        return findResponsavelEntities(true, -1, -1);
    }

    public List<Responsavel> findResponsavelEntities(int maxResults, int firstResult) {
        return findResponsavelEntities(false, maxResults, firstResult);
    }

    private List<Responsavel> findResponsavelEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Responsavel as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Responsavel findResponsavel(String cpf) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Responsavel as o where cpf = '" + cpf + "'");
            try{
                return (Responsavel) q.getSingleResult();
            } catch(NoResultException e){
                return null;
            }
        } finally {
            em.close();
        }
    }

    public int getResponsavelCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Responsavel as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
